{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136</td>\n",
       "      <td>20490</td>\n",
       "      <td>45</td>\n",
       "      <td>1981</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61</td>\n",
       "      <td>22778</td>\n",
       "      <td>86</td>\n",
       "      <td>1967</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139</td>\n",
       "      <td>2036500</td>\n",
       "      <td>2036500</td>\n",
       "      <td>34455</td>\n",
       "      <td>0.99</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5</td>\n",
       "      <td>15663</td>\n",
       "      <td>86</td>\n",
       "      <td>1977</td>\n",
       "      <td>2018</td>\n",
       "      <td>9</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76</td>\n",
       "      <td>17242</td>\n",
       "      <td>48</td>\n",
       "      <td>1995</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>13813</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>1974</td>\n",
       "      <td>2018</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>467161</td>\n",
       "      <td>550000</td>\n",
       "      <td>550000</td>\n",
       "      <td>12863</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     客户编号   已发货款   资产成本  贷款与资产比列   品牌  骑车销售商  车厂  出生日期  货款日期  地区  ...  \\\n",
       "0  601758  65532  78990    84.38  136  20490  45  1981  2018   8  ...   \n",
       "1  519488  56759  65325    89.55   61  22778  86  1967  2018   6  ...   \n",
       "2  447579  58413  67960    89.02    5  15663  86  1977  2018   9  ...   \n",
       "3  648134  72317  99750    73.68   76  17242  48  1995  2018   8  ...   \n",
       "4  458210  50078  65450    79.45  146  14181  45  1974  2018  17  ...   \n",
       "\n",
       "   尚未还清有效贷款总额  已批准贷款总额  已发放贷款总额  每月还款总额  贷款与已还贷款比列  主账户还款期数  次账户还款期数  \\\n",
       "0           0        0        0       0       1.00        0        0   \n",
       "1     2054139  2036500  2036500   34455       0.99       59        0   \n",
       "2           0        0        0       0       1.00        0        0   \n",
       "3           0    13813    13813       0   13814.00    13813        0   \n",
       "4      467161   550000   550000   12863       1.18       42        0   \n",
       "\n",
       "   贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0         1.0            1.00     0  \n",
       "1         1.0            1.33     1  \n",
       "2         1.0            1.00     1  \n",
       "3         1.0            2.00     0  \n",
       "4         1.0            1.06     1  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(r\"车贷违约预测.csv\",engine=\"python\")\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 异常值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 199717 entries, 0 to 199716\n",
      "Data columns (total 49 columns):\n",
      " #   Column          Non-Null Count   Dtype  \n",
      "---  ------          --------------   -----  \n",
      " 0   客户编号            199717 non-null  int64  \n",
      " 1   已发货款            199717 non-null  int64  \n",
      " 2   资产成本            199717 non-null  int64  \n",
      " 3   贷款与资产比列         199717 non-null  float64\n",
      " 4   品牌              199717 non-null  int64  \n",
      " 5   骑车销售商           199717 non-null  int64  \n",
      " 6   车厂              199717 non-null  int64  \n",
      " 7   出生日期            199717 non-null  int64  \n",
      " 8   货款日期            199717 non-null  int64  \n",
      " 9   地区              199717 non-null  int64  \n",
      " 10  对接员工编号          199717 non-null  int64  \n",
      " 11  是否填写手机号         199717 non-null  int64  \n",
      " 12  受否填写身份证         199717 non-null  int64  \n",
      " 13  是否出具驾驶证         199717 non-null  int64  \n",
      " 14  是否填写护照          199717 non-null  int64  \n",
      " 15  信用评分            199717 non-null  int64  \n",
      " 16  主账户贷款次数         199717 non-null  int64  \n",
      " 17  主账户有效贷款次数       199717 non-null  int64  \n",
      " 18  主账户中尚未还清有效贷款    199717 non-null  int64  \n",
      " 19  主账户中已批准的贷款      199717 non-null  int64  \n",
      " 20  主账户中已发放贷款       199717 non-null  int64  \n",
      " 21  次账户贷款次数         199717 non-null  int64  \n",
      " 22  次账户有效贷款次数       199717 non-null  int64  \n",
      " 23  次账户中尚未还清有效贷款    199717 non-null  int64  \n",
      " 24  次账户中已批准贷款       199717 non-null  int64  \n",
      " 25  次账户中已发放贷款       199717 non-null  int64  \n",
      " 26  主账户每月还款         199717 non-null  int64  \n",
      " 27  次账户没用还款         199717 non-null  int64  \n",
      " 28  近六个月新贷款次数       199717 non-null  int64  \n",
      " 29  近六个月违约次数        199717 non-null  int64  \n",
      " 30  平均贷款期限          199717 non-null  int64  \n",
      " 31  第一次贷款距今时间       199717 non-null  int64  \n",
      " 32  贷款查询次数          199717 non-null  int64  \n",
      " 33  是否违约            199717 non-null  int64  \n",
      " 34  贷款与资产比          199717 non-null  float64\n",
      " 35  贷款总次数           199717 non-null  int64  \n",
      " 36  主账户无效贷款次数       199717 non-null  int64  \n",
      " 37  次账户无效贷款次数       199717 non-null  int64  \n",
      " 38  无效贷款总次数         199717 non-null  int64  \n",
      " 39  尚未还清有效贷款总额      199717 non-null  int64  \n",
      " 40  已批准贷款总额         199717 non-null  int64  \n",
      " 41  已发放贷款总额         199717 non-null  int64  \n",
      " 42  每月还款总额          199717 non-null  int64  \n",
      " 43  贷款与已还贷款比列       199717 non-null  float64\n",
      " 44  主账户还款期数         199717 non-null  int64  \n",
      " 45  次账户还款期数         199717 non-null  int64  \n",
      " 46  贷款与已批准贷款比列      199717 non-null  float64\n",
      " 47  总贷款次数与总有效贷款次数比  199717 non-null  float64\n",
      " 48  工作类型            199717 non-null  int64  \n",
      "dtypes: float64(5), int64(44)\n",
      "memory usage: 74.7 MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-11-5aa435965fe1>:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  data.车厂[~(data.车厂.isin(chechang))] = \"其他\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(319.2260574534636,\n",
       " 3.3127156690441164e-64,\n",
       " 8,\n",
       " array([[3.97895969e+04, 1.15954963e+04, 7.20605477e+03, 1.92128556e+04,\n",
       "         1.67318669e+03, 7.74601531e+04, 6.79228244e+03, 5.46212371e+02,\n",
       "         1.31617439e+01],\n",
       "        [8.58040307e+03, 2.50050365e+03, 1.55394523e+03, 4.14314439e+03,\n",
       "         3.60813311e+02, 1.67038469e+04, 1.46471756e+03, 1.17787629e+02,\n",
       "         2.83825613e+00]]))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chechang = data.车厂.value_counts()[data.车厂.value_counts()>20].index\n",
    "data.车厂[~(data.车厂.isin(chechang))] = \"其他\"\n",
    "crosstable = pd.crosstab(data.是否违约,data.车厂)\n",
    "stats.chi2_contingency(crosstable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>车厂</th>\n",
       "      <th>45</th>\n",
       "      <th>48</th>\n",
       "      <th>49</th>\n",
       "      <th>51</th>\n",
       "      <th>67</th>\n",
       "      <th>86</th>\n",
       "      <th>120</th>\n",
       "      <th>145</th>\n",
       "      <th>其他</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>是否违约</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>39317</td>\n",
       "      <td>10951</td>\n",
       "      <td>7172</td>\n",
       "      <td>19392</td>\n",
       "      <td>1692</td>\n",
       "      <td>78441</td>\n",
       "      <td>6749</td>\n",
       "      <td>561</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9053</td>\n",
       "      <td>3145</td>\n",
       "      <td>1588</td>\n",
       "      <td>3964</td>\n",
       "      <td>342</td>\n",
       "      <td>15723</td>\n",
       "      <td>1508</td>\n",
       "      <td>103</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "车厂       45     48    49     51    67     86   120  145  其他\n",
       "是否违约                                                       \n",
       "0     39317  10951  7172  19392  1692  78441  6749  561  14\n",
       "1      9053   3145  1588   3964   342  15723  1508  103   2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "crosstable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2774.3510217293656,\n",
       " 2.174780140697608e-285,\n",
       " 571,\n",
       " array([[8.20075356e+04, 2.46782698e+00, 6.84410681e+02, ...,\n",
       "         4.77113215e+01, 8.22608992e-01, 3.29043597e+00],\n",
       "        [1.76844644e+04, 5.32173025e-01, 1.47589319e+02, ...,\n",
       "         1.02886785e+01, 1.77391008e-01, 7.09564033e-01]]))"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "crosstable = pd.crosstab(data.是否违约,data.信用评分)\n",
    "stats.chi2_contingency(crosstable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>信用评分</th>\n",
       "      <th>0</th>\n",
       "      <th>11</th>\n",
       "      <th>14</th>\n",
       "      <th>15</th>\n",
       "      <th>16</th>\n",
       "      <th>17</th>\n",
       "      <th>18</th>\n",
       "      <th>300</th>\n",
       "      <th>301</th>\n",
       "      <th>302</th>\n",
       "      <th>...</th>\n",
       "      <th>864</th>\n",
       "      <th>867</th>\n",
       "      <th>868</th>\n",
       "      <th>869</th>\n",
       "      <th>870</th>\n",
       "      <th>873</th>\n",
       "      <th>878</th>\n",
       "      <th>879</th>\n",
       "      <th>884</th>\n",
       "      <th>890</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>是否违约</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>80774</td>\n",
       "      <td>3</td>\n",
       "      <td>688</td>\n",
       "      <td>2527</td>\n",
       "      <td>2133</td>\n",
       "      <td>2617</td>\n",
       "      <td>1125</td>\n",
       "      <td>5505</td>\n",
       "      <td>9</td>\n",
       "      <td>15</td>\n",
       "      <td>...</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>19</td>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>53</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>18918</td>\n",
       "      <td>0</td>\n",
       "      <td>144</td>\n",
       "      <td>665</td>\n",
       "      <td>378</td>\n",
       "      <td>545</td>\n",
       "      <td>216</td>\n",
       "      <td>1849</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 572 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "信用评分    0    11   14    15    16    17    18    300  301  302  ...  864  867  \\\n",
       "是否违约                                                           ...             \n",
       "0     80774    3  688  2527  2133  2617  1125  5505    9   15  ...    9    1   \n",
       "1     18918    0  144   665   378   545   216  1849    0    2  ...    0    0   \n",
       "\n",
       "信用评分  868  869  870  873  878  879  884  890  \n",
       "是否违约                                          \n",
       "0       1    7   19    5    7   53    1    4  \n",
       "1       0    0    4    2    0    5    0    0  \n",
       "\n",
       "[2 rows x 572 columns]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "crosstable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    108941\n",
       "1     84195\n",
       "2      6581\n",
       "Name: 工作类型, dtype: int64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.工作类型.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(147.771914845079,\n",
       " 8.160868900928315e-33,\n",
       " 2,\n",
       " array([[89615.84616733, 69259.56405814,  5413.58977453],\n",
       "        [19325.15383267, 14935.43594186,  1167.41022547]]))"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "crosstable = pd.crosstab(data.是否违约,data.工作类型)\n",
    "stats.chi2_contingency(crosstable)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "客户编号              0.0\n",
       "已发货款              0.0\n",
       "资产成本              0.0\n",
       "贷款与资产比列           0.0\n",
       "品牌                0.0\n",
       "骑车销售商             0.0\n",
       "车厂                0.0\n",
       "出生日期              0.0\n",
       "货款日期              0.0\n",
       "地区                0.0\n",
       "对接员工编号            0.0\n",
       "是否填写手机号           0.0\n",
       "受否填写身份证           0.0\n",
       "是否出具驾驶证           0.0\n",
       "是否填写护照            0.0\n",
       "信用评分              0.0\n",
       "主账户贷款次数           0.0\n",
       "主账户有效贷款次数         0.0\n",
       "主账户中尚未还清有效贷款      0.0\n",
       "主账户中已批准的贷款        0.0\n",
       "主账户中已发放贷款         0.0\n",
       "次账户贷款次数           0.0\n",
       "次账户有效贷款次数         0.0\n",
       "次账户中尚未还清有效贷款      0.0\n",
       "次账户中已批准贷款         0.0\n",
       "次账户中已发放贷款         0.0\n",
       "主账户每月还款           0.0\n",
       "次账户没用还款           0.0\n",
       "近六个月新贷款次数         0.0\n",
       "近六个月违约次数          0.0\n",
       "平均贷款期限            0.0\n",
       "第一次贷款距今时间         0.0\n",
       "贷款查询次数            0.0\n",
       "是否违约              0.0\n",
       "贷款与资产比            0.0\n",
       "贷款总次数             0.0\n",
       "主账户无效贷款次数         0.0\n",
       "次账户无效贷款次数         0.0\n",
       "无效贷款总次数           0.0\n",
       "尚未还清有效贷款总额        0.0\n",
       "已批准贷款总额           0.0\n",
       "已发放贷款总额           0.0\n",
       "每月还款总额            0.0\n",
       "贷款与已还贷款比列         0.0\n",
       "主账户还款期数           0.0\n",
       "次账户还款期数           0.0\n",
       "贷款与已批准贷款比列        0.0\n",
       "总贷款次数与总有效贷款次数比    0.0\n",
       "工作类型              0.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.apply(lambda x : x.isna().sum()/x.size,axis=0)\n",
    "#无缺失值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 衍生字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#比例字段都给了，找不到可以衍生的字段"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 失衡数据判断并处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    164289\n",
       "1     35428\n",
       "Name: 是否违约, dtype: int64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.是否违约.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.17739100827671156"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "35428/data.是否违约.size\n",
    "#17.7% 不算失衡"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多个备选模型比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using matplotlib backend: TkAgg\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import time\n",
    "\n",
    "#模型处理模块\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "#常规模型\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "#集成学习模型和stacking模型\n",
    "from sklearn.ensemble import AdaBoostClassifier,GradientBoostingClassifier,RandomForestClassifier\n",
    "\n",
    "#评价标准模块\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import accuracy_score,roc_auc_score,recall_score,precision_score,classification_report\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in data.columns:\n",
    "    if data[i].dtype==np.float64:\n",
    "        data[i]=data[i].astype(\"float32\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = train_test_split(data.iloc[:,1:],data.是否违约,test_size=0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_model(X_train,y_train,X_test,y_test,model,model_name):\n",
    "    \n",
    "    print('训练{}'.format(model_name))\n",
    "    \n",
    "    #创建指定模型\n",
    "    clf=model\n",
    "    start = time.time()\n",
    "    \n",
    "    #训练模型\n",
    "    clf.fit(X_train,y_train.values.ravel())\n",
    "    \n",
    "    #验证模型\n",
    "    print('训练集评估')\n",
    "    train_pre = clf.predict(X_train)\n",
    "    print(classification_report(y_train,train_pre))\n",
    "    \n",
    "    print('检验集评估')\n",
    "    test_pre = clf.predict(X_test)\n",
    "    print(classification_report(y_test,test_pre))\n",
    "    \n",
    "    end = time.time()\n",
    "    duration = end - start\n",
    "    print('模型训练耗时：{:6f}s'.format(duration))\n",
    "    \n",
    "    return clf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name_param_dict = {'LR':(LogisticRegression()),\n",
    "                        'DT':(DecisionTreeClassifier()),\n",
    "                        'AB':(AdaBoostClassifier()),\n",
    "                        'RF':(RandomForestClassifier())\n",
    "                        }\n",
    "result = {}\n",
    "for model_name,model in model_name_param_dict.items():\n",
    "    result[model_name] = train_model(X_train,y_train,X_test,y_test,model,model_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 利用网格搜索调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid = {'n_estimators':[20,50,100,300],'max_depth':[4,6,8,10,12],\n",
    "             'criterion':[\"gini\",\"entropy\"],\"max_features\":[5,10,15,20]}\n",
    "model = RandomForestClassifier()\n",
    "grid_search = GridSearchCV(model,param_grid,cv=3,scoring='roc_auc')\n",
    "temp=grid_search.fit(X_train,y_train)\n",
    "temp.best_params_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 优质模型保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'joblib' from 'sklearn.externals' (c:\\users\\86173\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\sklearn\\externals\\__init__.py)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-985ca8ff1659>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexternals\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mjoblib\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'joblib' from 'sklearn.externals' (c:\\users\\86173\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\sklearn\\externals\\__init__.py)"
     ]
    }
   ],
   "source": [
    "from sklearn.externals import joblib\n",
    "joblib.dump(temp,'model.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
